package okl.lab.namesFilter.tests.cases
{
	import flash.events.EventDispatcher;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.ResultEvent;
	
	import net.digitalprimates.fluint.async.AsyncHandler;
	
	import okl.lab.namesFilter.controller.events.UsersEvent;
	import okl.lab.namesFilter.model.AppModel;
	import okl.lab.namesFilter.model.vo.User;
	import okl.lab.namesFilter.service.MockUserService;
	import okl.lab.namesFilter.tests.utils.TestDataGenerator;
	
	import org.flexunit.asserts.assertEquals;
	import org.flexunit.asserts.assertNotNull;
	import org.flexunit.asserts.assertTrue;
	import org.flexunit.asserts.fail;
	import org.flexunit.async.Async;
	
	/**
	 * Mock service returns random results.
	 * It should return some results all time
	 */
	public class MockUserServiceTest
	{		
		private var service:MockUserService;
		private var dispatcher:EventDispatcher;		
		/**
		 *Need to setup service and its dependencies
		 */
		[Before]
		public function setUp():void
		{
			dispatcher=new EventDispatcher();
			service=new MockUserService();
			service.model = new AppModel();
			service.model.eventDispatcher=dispatcher;
			service.eventDispatcher=dispatcher;
		}
		
		
		[After]
		public function tearDown():void
		{
			service=null;
			dispatcher=null;
		}
		
		/**
		 * Test fetching users
		 */
		[Test(async)]
		public function testGettingUsers():void
		{
			var handler:Function=Async.asyncHandler(this, gettingUsersResult, 8000, null, handleServiceTimeout)
			dispatcher.addEventListener( UsersEvent.UPDATED, handler, false, 0, true);
			this.service.getUsers();
		}
		
		//{region ASYNC_CALLBACKS
		protected function handleServiceTimeout( event:Object ):void
		{
			fail("Service Timeout");
		}
		
		protected function gettingUsersResult(event:UsersEvent, obj:Object):void{
			//the purpose of mocking service is to return some results
			assertNotNull(event.users);
			assertNotNull(event.users.length>0);
		}
		//}endregion
		
	}
}